﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Imports AugmentLib.WebComponent
Imports Lsk.Data
Imports Lsk.Business
Imports FlickrNet
Partial Class NewLaunchEntry
    Inherits System.Web.UI.Page

    Protected Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
        Dim ProjectId As Guid
        'Check if should update or save
        Dim savemode As Boolean = False
        If (lblhidPropertyId.Text = "") Then 'safe mode, new property launch
            savemode = True

        End If
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()

            objDB.AddParameter("ProjectName", Data.SqlDbType.NVarChar, txtProjectName.Text) 'lenght=10
            objDB.AddParameter("ProjectDetails", Data.SqlDbType.NVarChar, txtDescription.Text) '50
            objDB.AddParameter("ProjectLocation", Data.SqlDbType.NVarChar, txtLocation.Text) '300
            'objDB.AddParameter("ProjectPrice", Data.SqlDbType.Decimal, Convert.ToDecimal(txtPrice.Text))
            objDB.AddParameter("ProjectPrice", Data.SqlDbType.NVarChar, Trim(txtPrice.Text))
            objDB.AddParameter("Website", Data.SqlDbType.NVarChar, txtWebsite.Text) '50
            objDB.AddParameter("CreateWho", Data.SqlDbType.UniqueIdentifier, New Guid(lblhidStaffId.Text))
            objDB.AddParameter("UpdateWho", Data.SqlDbType.UniqueIdentifier, New Guid(lblhidStaffId.Text))

            If (savemode) Then
                ProjectId = Guid.NewGuid
                objDB.AddParameter("ProjectId", Data.SqlDbType.UniqueIdentifier, ProjectId)
                objDB.dbVariable.SQLString = "INSERT INTO Mobile_NewLaunch (ProjectId,ProjectName,ProjectDetails,ProjectLocation,ProjectPrice,Website,CreateWho,UpdateWho) " & _
                    "VALUES (@ProjectId,@ProjectName,@ProjectDetails,@ProjectLocation,@ProjectPrice,@Website,@CreateWho,@UpdateWho)"

            Else
                objDB.AddParameter("ProjectId", Data.SqlDbType.UniqueIdentifier, New Guid(lblhidPropertyId.Text))
                objDB.dbVariable.SQLString = "UPDATE Mobile_NewLaunch SET ProjectName=@ProjectName,ProjectDetails=@ProjectDetails,ProjectPrice=@ProjectPrice,Website=@Website,CreateWho=@CreateWho,UpdateWho=@UpdateWho" & _
                    " WHERE ProjectId=@ProjectId"
            End If

            objDB.ExecuteQuery(False)
            objDB.DbTransCommit()
            objDB.CloseConnection()


            'Success, alert success. Clear Textbox
            If (savemode) Then
                lblMsgSuccessful.Text = "Property added successfully."
                lblhidPropertyId.Text = ProjectId.ToString
            Else
                lblMsgSuccessful.Text = "Property updated successfully."
            End If

            divSuccessful.Visible = True
            udpMessage.Update()
            'ClearForm()
            upPropertyDetail.Update()
        Catch ex As Exception

            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.DbTransRollback()
                objDB.CloseConnection()
            End If
            lblMsgError.Text = ex.Message
            divError.Visible = True
            udpMessage.Update()

        End Try
    End Sub

    Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Response.Redirect("NewLaunch.aspx")
    End Sub

   

    Private Sub LoadData()

        'Return 1 if the AuctionID exist, 0 if the property never exist.
        Dim retval = 0

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT ProjectName,ProjectLocation,ProjectDetails, Website,ProjectPrice" & _
                                      " FROM Mobile_NewLaunch WHERE StatusDelete != 1 AND ProjectId = @ProjectId"
        objDB.AddParameter("@ProjectId", Data.SqlDbType.UniqueIdentifier, New Guid(lblhidPropertyId.Text))
        objDB.FillData(TempDT, "Mobile_NewLaunch", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        For Each ReadRow As Data.DataRow In TempDT.Rows
            txtProjectName.Text = ReadRow.Item("ProjectName").ToString
            txtDescription.Text = ReadRow.Item("ProjectDetails").ToString
            txtLocation.Text = ReadRow.Item("ProjectLocation").ToString
            txtPrice.Text = ReadRow.Item("ProjectPrice").ToString
            txtWebsite.Text = ReadRow.Item("Website").ToString
           
        Next

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")

        Else
            lblhidStaffId.Text = Session("StaffId").ToString
            lblhidStaffType.Text = Session("StaffType").ToString
        End If

        If (lblhidStaffType.Text = "4" Or lblhidStaffType.Text = "6") Then
            Response.Redirect("Dashboard.aspx")
        End If

        If Not Page.IsPostBack Then
            If Not Request.QueryString("id") Is Nothing Then ' Need to load existing auction property
                lblhidPropertyId.Text = Request.QueryString("id") 'SymmetricDecryptFromHex(Request.QueryString("id"), "augment")
                LoadData()
            End If
        End If
    End Sub

    Protected Sub btnUploadPhoto_Click(sender As Object, e As EventArgs) Handles btnUploadPhoto.Click
        Dim url As String
        If String.IsNullOrEmpty(lblhidPropertyId.Text) Then
            lblMsgError.Text = "Please save the entry first!."
            divError.Visible = True
        Else
            url = "UploadPhoto.aspx?id=" + lblhidPropertyId.Text
            Response.Redirect(url)
        End If


    End Sub
End Class
